tinymce.init(tinyMceConfig);
$(function () {
    'use strict';

    // Initialize the jQuery File Upload widget:
    $('#post-reply-form').fileupload({
        // Uncomment the following to send cross-domain cookies:
        //xhrFields: {withCredentials: true},
        url: post_reply_upload_url,
        autoUpload: true,
        previewMaxWidth: 120,
        previewMaxHeight: 90,

        acceptFileTypes: /(\.|\/)(gif|jpe?g|png|bmp)$/i,
        maxFileSize: 10240000, // 10 MB
        minFileSize: undefined, // No minimal file size
        maxNumberOfFiles: 20,

    });
    $('#post-reply-form').bind('fileuploaddestroy', function (e, data) { $('#ajax-loading').fadeIn('fast'); });
    $('#post-reply-form').bind('fileuploaddestroyed', function (e, data) { $('#ajax-loading').fadeOut('fast'); });

    // Load existing files: 
    $('#post-reply-form').addClass('fileupload-processing');
    $.ajax({
        // Uncomment the following to send cross-domain cookies:
        //xhrFields: {withCredentials: true},
        url: $('#post-reply-form').fileupload('option', 'url'),
        dataType: 'json',
        context: $('#post-reply-form')[0]
    }).always(function () {
        $(this).removeClass('fileupload-processing');
    }).done(function (result) {
        $(this).fileupload('option', 'done').call(this, $.Event('done'), {result: result});
    });
    
    
    $('.add_to_content').live('click', function(e){
        e.preventDefault();
        tinyMCE.execCommand('mceInsertRawHTML',false, '<img src="' + $(this).attr('url') + '" />');
    });
    
    $("#upload-grid").mCustomScrollbar({
        scrollInertia:300,
        horizontalScroll:false,
        advanced:{
            updateOnBrowserResize:true, /*update scrollbars on browser resize (for layouts based on percentages): boolean*/
            updateOnContentResize:true, /*auto-update scrollbars on content resize (for dynamic content): boolean*/
            autoExpandHorizontalScroll:false, /*auto-expand width for horizontal scrolling: boolean*/
            autoScrollOnFocus:true, /*auto-scroll on focused elements: boolean*/
            normalizeMouseWheelDelta:false /*normalize mouse-wheel delta (-1/1)*/
        },
        scrollButtons:{
            enable:true
        },
        theme:"dark-thick"
    });

});


$('#post-reply-form').submit(function(){
    $('#PostReply_content').val(tinyMCE.activeEditor.getContent());
});

function postReplyAfterValidate(form, data, hasError){
    if(hasError){
        var focusSelector;
        $.each(data, function(key, value){
            focusSelector = '#'+key;
            $.each(value, function(i, msg){
                myGrowl(msg, 'error');
            });        
        });    
        $(focusSelector).focus();
        scrollToAnchor(focusSelector);    
        return false;
        
    }else{
        $('#ajax-loading').fadeIn();
        var msg = ($('#post-reply-form').attr('actionType') == 'create' ? 'Trả lời của bạn đang được tải lên.'
        : 'Trả lời của bạn đang được cập nhật.')+' Xin vui lòng chờ trong giây lát.';
        myGrowl(msg, 'info');
        return true;
    }
}

$('#reply_to_user_remove').click(function(){
    $(this).parent().parent().fadeOut();
    $('#PostReply_reply_id').val('');    
});